﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class trendapplist : System.Web.UI.Page
    {
        protected string[] orders_Tendency = new string[] { "", "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_BUSNIESE_NUM] != null)
            {
                string businessNum = Session[DTKeys.SESSION_BUSNIESE_NUM].ToString(); 
                orders_Tendency = GetOrderTrendTimesDataByBusinessNum(12, businessNum);
            }
        }

        private string[] GetOrderTrendTimesDataByBusinessNum(int nmonth, string businessNum)
        {
            //获取最近n天的统计数据
            DateTime startday = DateTime.Now.AddMonths(-nmonth);
            DateTime endDay = DateTime.Now.AddDays(0);
            string strStartTime = startday.ToString("yy-MM");
            string strEndTime = endDay.ToString("yy-MM");
            BLL.sf_appidmanage bll_app = new BLL.sf_appidmanage();
            System.Data.DataSet dsAppList = bll_app.GetList(" businessNum = '" + businessNum + "'");


            BLL.statistics bll = new BLL.statistics();
            System.Data.DataSet dsOrders = bll.GetOrderTrendTimesDataByBusinessNum(businessNum, strStartTime, strEndTime);

            string xAxis = "";
            for (int i = 0; i < nmonth; i++)
            {
                xAxis += "'" + startday.AddMonths(i).ToString("yy-MM") + "',";
            }
            if (xAxis.Length > 0) 
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
            }

            string yAxis = "";
            foreach (System.Data.DataRow r in dsAppList.Tables[0].Rows)
            {
                string temp_yAxis = string.Empty;
                for (int i = 0; i < nmonth; i++)
                {
                    string strTime = startday.AddMonths(i).ToString("yy-MM");
                    System.Data.DataRow[] rows = dsOrders.Tables[0].Select("appNum='"+r["appid"].ToString()+"' and day = '" + strTime + "'");
                    if (rows.Length > 0)
                    {
                        temp_yAxis += rows[0]["count"].ToString() + ",";
                    }
                    else
                    {
                        temp_yAxis += "0,";
                    }
                }

                if (temp_yAxis.Length > 0) 
                {
                    temp_yAxis = temp_yAxis.Remove(temp_yAxis.Length - 1 ,1 );
                }

                yAxis += "{ type: 'spline',name:'" + r["appid_name"].ToString() + "', data: ["+temp_yAxis+"] },";
            }


            if (yAxis.Length > 0)
            {
                yAxis = yAxis.Remove(yAxis.Length - 1, 1);
            }
            return new[] { xAxis, yAxis};
        }
    }
}